// pages/lvju/lvju.js
// var common = require("../header/header.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hostUrl: app.globalData.hostUrl,
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    num: 2,

    // 三联属性
    array: ['省'],
    shiarray: ['市'],
    quarray: ['区'],
    index: 0,
    shiindex: 0,
    quindex: 0,
    clickShow: false,
  },
  // 头部导航
  // nav: function(e) {
  //   console.log(e);
  //   this.setData({
  //     num: e.target.dataset.num
  //   })
  // },
  // 搜索框三联

  // bindRegionChange: function (e) {
  //   console.log(e)

  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   // this.setData({
  //   //   region: e.detail.value
  //   // })
  //   wx.navigateTo({
  //     url: '../new_inter/hotel_cityils/hotel_cityils?quid=' + e.detail.code[2],
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      foot:wx.getStorageSync('foot'),
      is_show:wx.getStorageSync('is_show')

    })

    this.lvju_news();
  },

  // 三联开始
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为省', e.detail.value)
    var index = e.detail.value;
    if (this.data.info['province'][index]['AreaName']=="请选择"){
      return ;
    }
    this.setData({
      index: e.detail.value
    })
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/getarea',
      data: {
        ParentId: that.data.info['province'][index]['AreaId'],
        type: 'hotel'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        if (res.data != null) {
          that.setData({
            shiarray: res.data,
          })
        }
      }
    })
  },
  bindShiChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var shiindex = e.detail.value;
    if (this.data.shiarray[shiindex]['AreaName'] == "请选择") {
      return;
    }
    this.setData({
      shiindex: e.detail.value
    })
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/getarea',
      data: {
        ParentId: that.data.shiarray[shiindex]['AreaId'],
        type: 'hotel'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res)
        if (res.data != null) {
          that.setData({
            quarray: res.data,
          })
        }
      }
    })
  },
  bindQuChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var code = this.data.quarray[e.detail.value]['AreaCode'];
    if (this.data.quarray[e.detail.value]['AreaName'] == "请选择") {
      return;
    }
    this.setData({
      quindex: e.detail.value
    })
    wx.navigateTo({
      url: '../new_inter/hotel_cityils/hotel_cityils?quid=' + code,
    })
  },

  // 点击显示三联
  clickCityPicker:function(){
    this.setData({
      clickShow:true
    })
  },
  // 三联结束

  lvju_news: function(typeId) { //读取首页banner
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Index/lvju_news',
      data: {
        // opt: 'getBanner',
        typeId: '',
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res)
        if (res.data != null) {

          var adv = res.data.adv[1];
          for (var i = 0, len = adv.length; i < len; ++i) {
            var desc = that.delHtmlTag(res.data.adv[1][i].desc)
            res.data.adv[1][i].desc = desc;
          }
          that.setData({
            info: res.data,
            array:res.data.province,
            recom: res.data.recom
          })
          that.setCountDown();

          // console.log(that.data.info, '过滤后')
        }
      }
    })
    // console.log(that.data.imgBanner['0'])

  },
  delHtmlTag: function(msg) {
    var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
    // msg = msg.replace(/[|]*\n/, '') //去除行尾空格
    // msg = msg.replace(/&nbsp;/ig, ''); //去掉npsp
    return msg;
  },
  /**
   * 倒计时
   */
  setCountDown: function () {
    // var that=this;
    let time = 1000;
    let miaosha = this.data.recom;
    for (let i = 0; i < miaosha.length; i++) {

      for (let s = 0; s < miaosha[i].length; s++) {
        // console.log(miaosha[i][s]['prefer'])
        var timestamp = (new Date()).getTime() / 1000;
        var end_time = miaosha[i][s].prefer_end - timestamp;

        if (miaosha[i][s]['prefer'] == 1 && end_time>0) {
          
          let dao_time = this.getFormat(end_time);
          // console.log(dao_time, '时间')

          miaosha[i][s]['dao_time'] = dao_time;
          miaosha[i][s]['is_dao'] = 0;
          // console.log(this.data.recom, '更新后数组')
        } else {
          miaosha[i][s]['is_dao'] = 1;
          this.setData({
            recom: miaosha
          })
          continue;
        }
        this.setData({
          recom: miaosha
        })
      }

    }
          // console.log(this.data.recom, '更新后数组')

    setTimeout(this.setCountDown, time);
  },

  /**
   * 格式化时间
   */
  getFormat: function (second_time) {
    // console.log(parseInt(second_time))
    if (parseInt(second_time) > 60) {

      var second = parseInt(second_time) % 60;
      var min = parseInt(second_time / 60);
      // var time = min + "分" + second + "秒";

      if (min > 60) {
        min = parseInt(second_time / 60) % 60;
        var hour = parseInt(parseInt(second_time / 60) / 60);
        // var time = hour + "小时" + min + "分" + second + "秒";

        if (hour > 24) {
          hour = parseInt(parseInt(second_time / 60) / 60) % 24;
          var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24);
          var time = day + "天" + hour + "小时" + min + "分" + second + "秒";
        }
      }


    }

    return time;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  index: function() {
    wx.reLaunch({
      url: '../index/index'
    })
  },

  // dichan: function() {
  //   wx.reLaunch({
  //     url: '../dichan/dichan'
  //   })
  // },
  // jiankang: function() {
  //   wx.navigateTo({
  //     url: '../jiankang/jiankang'
  //   });
  // },
  // news: function() {
  //   wx.navigateTo({
  //     url: '../news/news'
  //   });
  // },
})